home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 April: Mac OS SDK / Dev.CD Apr 98 SDK2.toast / Development Kits (Disc 2) / QuickTime™ IC SDK 1.01 / Sample Code / QTICSampleApp / Sources / qtic.c < prev   
Encoding:
Text File  |  1997-06-06  |  11.2 KB  |  398 lines  |  [TEXT/CWIE]

  1. //========================================================================
  2. // Application:        QTICSampleApp
  3. //
  4. // Description:        This application demonstrates the integration of
  5. //                    QuickTime™ IC functionality.
  6. //
  7. // Author:            Mike Bitz
  8. //                     Developer Technical Support
  9. //                     Apple Computer, Inc.
  10. //
  11. // History:            25-Apr-97 original development (mwb)
  12. //
  13. // Copyright © 1997 Apple Computer, Inc., All Rights Reserved
  14. //
  15. // You may incorporate this sample code into your applications without
  16. // restriction, though the sample code has been provided "AS IS" and the
  17. // responsibility for its operation is 100% yours.  However, what you are
  18. // not permitted to do is to redistribute the source as "DSC Sample Code"
  19. // after having made changes. If you're going to re-distribute the source,
  20. // we require that you make it clear in the source that the code was
  21. // descended from Apple Sample Code, but that you've made changes.
  22. //========================================================================
  23.  
  24. #include "prototypes.h"
  25. #include "application.h"
  26. #include <QTIC.h>
  27. #include "IterateDirectory.h" // from MoreFiles
  28. #include <PLStringFuncs.h>
  29.  
  30. extern QTICControls                            gCameraControls;
  31. extern QTICActionFilterWithRefConUPP         gCallBack;
  32. extern MenuHandle                            gPanelSpecificMenu;
  33. extern Component                            gPanelComponents[10];
  34. extern short                                gNumPanels, gNumPrefPanels;
  35.  
  36.  
  37. //================= myIterateDirectory =================
  38. //
  39. // We use this function to help us register 'qtic' components
  40. // at start-up time.  It's passed as a parameter to 
  41. // FSpIterateDirectory below.
  42. //
  43. pascal void myIterateDirectory(const CInfoPBRec * const cpbPtr, Boolean *quitFlag, void *yourDataPtr)
  44. {
  45.     short            registerResRefNum;
  46.     FSSpec            myFSSpec;
  47.     
  48.     if ( (cpbPtr->hFileInfo.ioFlAttrib & ioDirMask) == 0 && cpbPtr->hFileInfo.ioFlFndrInfo.fdType == 'qtic' ) {
  49.         if ( FSMakeFSSpec(cpbPtr->hFileInfo.ioVRefNum, cpbPtr->hFileInfo.ioFlParID, cpbPtr->hFileInfo.ioNamePtr,
  50.                 &myFSSpec) == noErr ) {
  51.             registerResRefNum = FSpOpenResFile(&myFSSpec, fsRdPerm);
  52.             if ( registerResRefNum != -1 ) {
  53.                 RegisterComponentResourceFile(registerResRefNum, 0);
  54.                 CloseResFile(registerResRefNum);
  55.             }
  56.         }
  57.     }
  58. }
  59.  
  60. //================= InitQTIC =================
  61. //
  62. // This is called when the application starts up.  We use FSpIterateDirectory
  63. // which is available with MoreFiles.  Check your Developer Tool Chest CD
  64. // for this fine file system library.
  65. //
  66. void InitQTIC (void) 
  67. {
  68.     OSErr                    err = noErr;
  69.     FCBPBRec                pb;
  70.     short                    resFile;
  71.     FSSpec                    myFSSpec;
  72.  
  73.     //    Load any components in the folder of the application.
  74.     pb.ioNamePtr = myFSSpec.name;
  75.     pb.ioVRefNum = 0;
  76.     pb.ioRefNum = CurResFile();
  77.     pb.ioFCBIndx = 0;
  78.     if (PBGetFCBInfoSync(&pb) == noErr) 
  79.     {
  80.         resFile = CurResFile();
  81.  
  82.         myFSSpec.vRefNum = pb.ioFCBVRefNum;
  83.         myFSSpec.parID = pb.ioFCBParID;
  84.         myFSSpec.name[0] = 0;
  85.         FSpIterateDirectory(&myFSSpec, 3, myIterateDirectory, nil);
  86.         UseResFile(resFile);
  87.     }
  88.  
  89.     // Open a QTIC camera controls component
  90.     gCameraControls = OpenDefaultComponent (kQTICControlsComponentType, kQTICControlsComponentSubType);
  91.     if (gCameraControls != nil) 
  92.     {
  93.         // You must call QTICCInitialize immediately after you open the camera controls component
  94.         err = QTICCInitialize (gCameraControls, nil);
  95.         if (err != noErr)
  96.         {
  97.             CloseComponent (gCameraControls);
  98.             gCameraControls = nil;
  99.             
  100.             // Use the QTIC facilities to report the error
  101.             QTICCReportError (gCameraControls, "\pQTICInitialize failed", err);
  102.         }
  103.  
  104.         // Set up a callback function so we can override actions
  105.         gCallBack = NewQTICActionFilterWithRefConProc (CameraCallBack);
  106.         if (gCallBack != nil) 
  107.         {
  108.             // Install an action filter (these are called in the order in which they are installed).
  109.             if (QTICCInstallActionFilter (gCameraControls, gCallBack, 0) != noErr) 
  110.             {
  111.                 CloseComponent (gCameraControls);
  112.                 gCameraControls = nil;
  113.             }
  114.         } 
  115.         else 
  116.         {
  117.             CloseComponent (gCameraControls);
  118.             gCameraControls = nil;
  119.         }
  120.     }
  121.     else
  122.     {
  123.         // Use the QTIC facilities to report the error
  124.         QTICCReportError (gCameraControls, "\pControls Component not opened", err);
  125.     }
  126. }
  127.  
  128. //================= CameraCallBack =================
  129. pascal Boolean CameraCallBack (QTICControls qtcc, short action, void *param1, void *param2, void *param3, void *param4, long refCon)
  130. {
  131.     Boolean                 handled = false;
  132.     
  133.     // Don't override anything for now.
  134.     switch (action) 
  135.     {
  136.         default:
  137.         break;
  138.     }
  139.     
  140.     return (handled);
  141. }
  142.  
  143. //================= OpenMainPanel =================
  144. OSErr OpenMainPanel (void)
  145. {
  146.     OSErr                err = noErr;
  147.     
  148.     if (gCameraControls != nil)
  149.     {
  150.         // If the panel is already open, don't bother opening it again.
  151.         err = QTICCDoAction(gCameraControls, kQTICActionIsPanelOpenByFlag, (void *) kQTICP_MainPanelFlag, nil, nil, nil);
  152.         if (err == 1)
  153.         {
  154.             // The panel is not open, so we'll open it now.
  155.             err = QTICCDoAction(gCameraControls, kQTICActionOpenPanelByFlag, (void *) kQTICP_MainPanelFlag, nil, nil, nil);
  156.         }
  157.     }
  158.     
  159.     // Enable Close Window item if a window is in the foreground
  160.     UpdateCloseWindowItem ();
  161.  
  162.     return err;    
  163. }
  164.  
  165. //================= CloseAppWindow =================
  166. OSErr CloseAppWindow (void)
  167. {
  168.     OSErr                err = noErr;
  169.     WindowPtr            theFrontWindow = nil;
  170.     MenuHandle            hMenu = nil;
  171.     
  172.     theFrontWindow = FrontWindow ();
  173.     if (theFrontWindow != nil)
  174.     {
  175.         if (gCameraControls != nil)
  176.         {
  177.             //It's a QTIC window, close it properly
  178.             if (theFrontWindow != nil && QTICCIsPanel (gCameraControls, theFrontWindow, nil) != 0)
  179.                 QTICCCloseWindow (gCameraControls, theFrontWindow);  
  180.             else
  181.                 // otherwise we'll close it the normal way
  182.                 CloseWindow (theFrontWindow);
  183.         }
  184.         
  185.         // Disable the Close Window menu item if no windows are currently visible.
  186.         UpdateCloseWindowItem ();
  187.     }    
  188.     return err;
  189. }
  190.  
  191. //================= NumberOfInstalledCameras =================
  192. //
  193. // Return the number of 'cmra' components which are installed.
  194. //
  195. short NumberOfInstalledCameras (void)
  196. {
  197.     short                    currentCount = 0;
  198.     ComponentDescription    compDesc;
  199.     Component                theComponent = nil;
  200.  
  201.     compDesc.componentType = kQTICCameraComponentType;
  202.     compDesc.componentSubType = 0;
  203.     compDesc.componentManufacturer = 0;
  204.     compDesc.componentFlags = 0L;
  205.     compDesc.componentFlagsMask = 0L;
  206.  
  207.     do
  208.     {
  209.         theComponent = FindNextComponent (theComponent, &compDesc);
  210.         if (theComponent != nil)
  211.         {
  212.             currentCount++;
  213.         }
  214.     }
  215.     while (theComponent != nil);
  216.         
  217.     return currentCount;
  218. }
  219.  
  220. //================= UpdatePanelSpecificMenu =================
  221. //
  222. // When a panel is the frontmost window, we need to
  223. // update our custom menu item to reflect the panel's
  224. // capabilities.
  225. //
  226. void UpdatePanelSpecificMenu (void)
  227. {
  228.     OSErr                    err = noErr;
  229.     Str255                     name;
  230.     WindowPtr                frontWindow = nil;
  231.         
  232.     if (gPanelSpecificMenu != nil)
  233.     {
  234.         DeleteMenu (menuTemp);
  235.         DisposeHandle ((Handle)gPanelSpecificMenu);
  236.         gPanelSpecificMenu = nil;
  237.     }
  238.     
  239.     frontWindow = FrontWindow ();
  240.     
  241.     if (frontWindow != nil) 
  242.     {
  243.         // Get the menu name from the window
  244.         err = QTICCGetMenuName (gCameraControls, frontWindow, name);
  245.         if (err == noErr) 
  246.         {
  247.             // Create a new menu item
  248.             gPanelSpecificMenu = NewMenu (menuTemp, name);
  249.             if (gPanelSpecificMenu != nil) 
  250.             {
  251.                 // Allow the panel that owns the window to update the custom menu
  252.                 err = QTICCUpdateCustomMenu (gCameraControls, frontWindow, gPanelSpecificMenu);
  253.                 if (err == noErr)
  254.                 {
  255.                     // Insert menu items if there are any
  256.                     if (CountMItems (gPanelSpecificMenu) > 0 ) 
  257.                     {
  258.                         InsertMenu (gPanelSpecificMenu, 0);
  259.                     } 
  260.                     // otherwise remove the menu because it would not provide any benefit to the user
  261.                     else 
  262.                     {
  263.                         DisposeHandle ((Handle)gPanelSpecificMenu);
  264.                         gPanelSpecificMenu = nil;
  265.                     }
  266.                 }
  267.             }
  268.         }
  269.     }
  270.     
  271.     // Update the Close Window menu item since we might have closed all the windows
  272.     UpdateCloseWindowItem ();
  273.  
  274.     DrawMenuBar ();
  275. }
  276.  
  277. //================= UpdatePanelListMenu =================
  278. //
  279. // Create a menu list of all available panel components.
  280. //
  281. void UpdatePanelListMenu (void)
  282. {
  283.     OSErr                        err = noErr;
  284.     MenuHandle                    hPanelListMenu = nil;
  285.     short                        i, numPanels;
  286.     Component                    **hPanelComponents = nil;
  287.     ComponentDescription        compDesc;
  288.     Handle                        componentName =  nil;
  289.     Str255                        menuString;
  290.     
  291.     if (gCameraControls != nil)
  292.     {
  293.         gNumPanels = 0;
  294.         gNumPrefPanels = 0;
  295.         
  296.         // Get a handle to the menu record, remove all of its items
  297.         hPanelListMenu = GetMHandle (menuPanels);
  298.         for (i = CountMItems (hPanelListMenu); i>0; i--)
  299.         {
  300.             DeleteMenuItem (hPanelListMenu, 1);
  301.         }
  302.         
  303.         // Get the number of installed panels that will work with the selected device.
  304.         err = QTICCGetNumPanels (gCameraControls, &numPanels);
  305.         if (numPanels > 0 && err == noErr)
  306.         {
  307.             // Allocate memory for the panel components list
  308.             hPanelComponents = (Component**)NewHandle (sizeof (Component) * numPanels);
  309.             if (hPanelComponents != nil)
  310.             {
  311.                 MoveHHi ((Handle)hPanelComponents);
  312.                 HLock ((Handle) hPanelComponents);
  313.                 
  314.                 // Get the list of installed panel components
  315.                 err = QTICCGetPanels (gCameraControls, (Component*) *hPanelComponents);
  316.                 if (err == noErr)
  317.                 {
  318.                     // Show/hide/main panels
  319.                     for (i=0; i<numPanels; i++)
  320.                     {
  321.                         componentName = NewHandle (4);
  322.                         if (componentName != nil)
  323.                         {
  324.                             err = GetComponentInfo ((*hPanelComponents)[i], &compDesc, componentName, nil, nil);
  325.                             if (err == noErr)
  326.                             {
  327.                                 HLock (componentName);
  328.                                 
  329.                                 if (compDesc.componentType == kQTICPanelComponentType &&
  330.                                     (compDesc.componentFlags & kQTICP_ShowHidePanelFlag) &&
  331.                                     (compDesc.componentFlags & kQTICP_MainPanelFlag))
  332.                                 {
  333.                                     AppendMenu (hPanelListMenu, (StringPtr)*componentName);
  334.                                     gPanelComponents [gNumPanels] = (*hPanelComponents)[i];
  335.                                     gNumPanels++;
  336.                                 }
  337.                             }
  338.                         }
  339.                         DisposeHandle (componentName);
  340.                     }
  341.                     
  342.                     // Show/hide/not main panels
  343.                     for (i=0; i<numPanels; i++)
  344.                     {
  345.                         componentName = NewHandle (4);
  346.                         if (componentName != nil)
  347.                         {
  348.                             err = GetComponentInfo ((*hPanelComponents)[i], &compDesc, componentName, nil, nil);
  349.                             if (err == noErr)
  350.                             {
  351.                                 HLock (componentName);
  352.                                 
  353.                                 if (compDesc.componentType == kQTICPanelComponentType &&
  354.                                     (compDesc.componentFlags & kQTICP_ShowHidePanelFlag) &&
  355.                                     !(compDesc.componentFlags & kQTICP_MainPanelFlag))
  356.                                 {
  357.                                     AppendMenu (hPanelListMenu, (StringPtr)*componentName);
  358.                                     gPanelComponents [gNumPanels] = (*hPanelComponents)[i];
  359.                                     gNumPanels++;
  360.                                 }
  361.                             }
  362.                         }
  363.                         DisposeHandle (componentName);
  364.                     }
  365.                     
  366.                     // Seperate the other panels from the preferences
  367.                     AppendMenu (hPanelListMenu, "\p-");
  368.                     gNumPrefPanels++;
  369.                 
  370.                     // Preference panels
  371.                     for (i=0; i<numPanels; i++) 
  372.                     {
  373.                         componentName = NewHandle(4);
  374.                         if (componentName != nil) 
  375.                         {
  376.                             err = GetComponentInfo((*hPanelComponents)[i], &compDesc, componentName, nil, nil);
  377.                             if (err == noErr)
  378.                             {
  379.                                 HLock (componentName);
  380.                                 
  381.                                 if (compDesc.componentType == kQTICPanelComponentType &&
  382.                                     (compDesc.componentFlags & kQTICP_NeedsNewMenuFlag) &&
  383.                                     !(compDesc.componentFlags & kQTICP_ShowHidePanelFlag)) 
  384.                                 {
  385.                                     AppendMenu(hPanelListMenu, (StringPtr)*componentName);
  386.                                     gPanelComponents [gNumPanels+gNumPrefPanels] = (*hPanelComponents)[i];
  387.                                     gNumPrefPanels++;
  388.                                 }
  389.                             }
  390.                         }
  391.                         DisposeHandle (componentName);
  392.                     }
  393.                 }
  394.                 DisposeHandle ((Handle)hPanelComponents);
  395.             }
  396.         }
  397.     }
  398. }